<template>
    <div class="login">
        <div class="container">
            <!-- 12列 - Bootstrap4 -->
            <div class="row">
                <div class="col-xs-12 col-sm-9 col-md-7 col-lg-5 m-auto">
                    <div class="card">
                        <div class="card-header">
                            <h3 class="text-center mb-0 text-secondary">{{ $conf.logo }}</h3>
                        </div>
                        <div class="card-form-body card-body">
                            <el-form ref="loginFormRefs" :model="loginForm" :rules="loginFormRules">
                                <el-form-item prop="username">
                                    <el-input prefix-icon="iconfont iconyonghu" v-model="loginForm.username"
                                        size="medium" placeholder="请输入用户名">
                                    </el-input>
                                </el-form-item>
                                <el-form-item prop="password">
                                    <el-input prefix-icon="iconfont iconmima" v-model="loginForm.password"
                                        size="medium" placeholder="请输入密码"
                                        type="password">
                                    </el-input>
                                </el-form-item>
                                <el-form-item>
                                    <el-button class="w-100" type="primary" size="medium" @click="submitForm()">
                                        火速登录
                                    </el-button>
                                    <el-button class="w-100 ml-0 mt-2" type="info" size="medium" @click="resetForm()">
                                        立即重置
                                    </el-button>
                                </el-form-item>
                            </el-form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    // 引入vuex辅助函数库
    import { mapMutations } from 'vuex';

    export default {
        name: 'login',
        watch: {

        },
        created() {

        },
        mounted() {

        },
        data() {
            return {
                // 表单数据
                loginForm: {
                    username: '',
                    password: ''
                },
                // 验证规则
                loginFormRules: {
                    username: [{
                        required: true,
                        message: '请输入用户名',
                        trigger: 'blur'
                    }],
                    password: [{
                        required: true,
                        message: '请输入密码',
                        trigger: 'blur'
                    }]
                }
            };
        },
        methods: {
            ...mapMutations({
                'changeGetToken': 'getTokenModule/updateGetToken'
            }),
            // 重置表单，清空数据
            resetForm() {
                this.$refs['loginFormRefs'].resetFields();
            },
            // 校验表单，没问题提交
            submitForm() {
                this.$refs['loginFormRefs'].validate(async valid => {
                    // 校验不通过直接返回，不再往下
                    if (!valid) return;

                    // 请求接口，获取数据
                    let res = await this.$axios.user.loginUser(this.loginForm);

                    let { meta: { msg, status } } = res.data;

                    // 接口请求成功
                    if (status == 200) {
                        // 登录成功提示消息
                        this.$message.success(msg);

                        // 将token存储在sessionStorage中
                        let { token } = res.data.data;

                        // 存储vuex同时存入sessionStorage中
                        this.changeGetToken({
                            token
                        });

                        // 跳转后台首页
                        this.$router.push({
                            name: 'layout'
                        });
                    } else {
                        // 登录失败提示消息
                        this.$message.error(msg);
                    }
                });
            }
        },
        computed: {

        },
        components: {

        }
    };
</script>

<style lang="scss" scoped>
    .login,
    .container,
    .row {
        height: 100%;
    }
</style>

<style lang="scss">
    .card-form-body {
        padding-bottom: 0;
        box-sizing: border-box;
        -webkit-box-sizing: border-box;
    }

    .el-form-item__content {
        .el-input {
            height: 40px;
            input {
                height: 40px;
            }

            .el-input__icon {
                line-height: 40px;
            }
        }
    }
</style>
